package com.zsy.petshop_01.manager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zsy.petshop_01.dao.mapper.SubscribeNumMapper;
import com.zsy.petshop_01.dao.mapper.SubscribeUserMapper;
import com.zsy.petshop_01.model.dto.ShowSubscribeUserDto;
import com.zsy.petshop_01.model.entity.SubscribeNum;
import com.zsy.petshop_01.model.entity.SubscribeUser;
import com.zsy.petshop_01.model.vo.subscribe.AnswerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Component
public class SubscribeManager {
    @Autowired
    SubscribeUserMapper subscribeUserMapper;
    @Autowired
    SubscribeNumMapper subscribeNumMapper;

    public void subscribe(String time, String message, Boolean isMorning, Integer userId) {
        LambdaQueryWrapper<SubscribeNum> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SubscribeNum::getCreateTime, time);
        SubscribeNum subscribeNum = subscribeNumMapper.selectOne(wrapper);
        if (isMorning) {
            subscribeNum.setMorningNum(subscribeNum.getMorningNum() + 1);
        } else {
            subscribeNum.setAfternoonNum(subscribeNum.getAfternoonNum() + 1);
        }
        subscribeNumMapper.updateById(subscribeNum);
        SubscribeUser subscribeUser = new SubscribeUser();
        subscribeUser.setSubscribeId(subscribeNum.getId());
        subscribeUser.setUserId(userId);
        subscribeUser.setIsMorning(isMorning);
        subscribeUser.setMessage(message);
        subscribeUserMapper.insert(subscribeUser);
    }

    public SubscribeNum selectSubscribeInDay(String time) {
        LambdaQueryWrapper<SubscribeNum> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SubscribeNum::getCreateTime, time);
        return subscribeNumMapper.selectOne(wrapper);
    }

    public List<ShowSubscribeUserDto> selectSubscribeByUserId(Integer userId) {
        List<ShowSubscribeUserDto> result = new ArrayList<>();
        LambdaQueryWrapper<SubscribeUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SubscribeUser::getUserId, userId);
        List<SubscribeUser> subscribeUsers = subscribeUserMapper.selectList(wrapper);
        for (SubscribeUser subscribeUser : subscribeUsers) {
            ShowSubscribeUserDto showSubscribeUserDto = new ShowSubscribeUserDto();
            Integer subscribeId = subscribeUser.getSubscribeId();
            SubscribeNum subscribeNum = subscribeNumMapper.selectById(subscribeId);
            String createTime = subscribeNum.getCreateTime();
            showSubscribeUserDto.setSubscribeId(subscribeUser.getId());
            showSubscribeUserDto.setTime(createTime);

            showSubscribeUserDto.setIsMorning(subscribeUser.getIsMorning());
            showSubscribeUserDto.setMessage(subscribeUser.getMessage());
            showSubscribeUserDto.setResponse(subscribeUser.getResponse());
            result.add(showSubscribeUserDto);
        }
        return result;
    }

    public void createSenvenDays(LocalDate time) {
        // 指定中国时区（东八区，Asia/Shanghai）
        ZoneId zoneId = ZoneId.of("Asia/Shanghai");
        for (int i = 0; i < 7; i++) {
            // 将 LocalDate 转换为中国时区的 LocalDateTime 并设置为当天 0 点
            LocalDateTime midnight = LocalDateTime.of(time, LocalTime.MIN).atZone(zoneId).toLocalDateTime();
            // 将 0 点时间转换为毫秒时间戳
            long epochMilli = midnight.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();

            SubscribeNum subscribeNum = new SubscribeNum();
            LambdaQueryWrapper<SubscribeNum> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SubscribeNum::getCreateTime, String.valueOf(epochMilli));
            SubscribeNum subscribeNum1 = subscribeNumMapper.selectOne(wrapper);
            if (subscribeNum1 != null) {
                time = time.plusDays(1);
                continue;
            }
            subscribeNum.setCreateTime(String.valueOf(epochMilli));
            subscribeNumMapper.insert(subscribeNum);
            time = time.plusDays(1);
        }
    }

    public List<ShowSubscribeUserDto> selectSubscribeByMorning(Integer id, Boolean isMorning) {
        List<ShowSubscribeUserDto> result = new ArrayList<>();
        LambdaQueryWrapper<SubscribeUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SubscribeUser::getSubscribeId, id)
                .eq(SubscribeUser::getIsMorning, isMorning);
        List<SubscribeUser> subscribeUsers = subscribeUserMapper.selectList(wrapper);
        for (SubscribeUser subscribeUser : subscribeUsers) {
            ShowSubscribeUserDto showSubscribeUserDto = new ShowSubscribeUserDto();
            Integer subscribeId = subscribeUser.getSubscribeId();
            SubscribeNum subscribeNum = subscribeNumMapper.selectById(subscribeId);
            String createTime = subscribeNum.getCreateTime();
            showSubscribeUserDto.setSubscribeId(subscribeUser.getId());
            showSubscribeUserDto.setTime(createTime);

            showSubscribeUserDto.setIsMorning(subscribeUser.getIsMorning());
            showSubscribeUserDto.setMessage(subscribeUser.getMessage());
            showSubscribeUserDto.setResponse(subscribeUser.getResponse());
            result.add(showSubscribeUserDto);
        }
        return result;
    }

    public void ownerAnswer(AnswerVo answerVo) {
        LambdaQueryWrapper<SubscribeUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SubscribeUser::getId, answerVo.getSubscribeId());
        SubscribeUser subscribeUser = subscribeUserMapper.selectOne(wrapper);
        subscribeUser.setResponse(answerVo.getAnswer());
        subscribeUserMapper.updateById(subscribeUser);
    }
}
